package com.rongzhe.house.presenter;

import android.content.Intent;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.LoginDataTo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.LoginActivity;
import com.rongzhe.house.ui.activity.MainActivity;
import com.rongzhe.house.utils.CheckParamUtil;

/**
 * Created by zhongyanli on 2017/9/8.
 */

public class LoginPresenter extends BasePresenter {
    public LoginPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        super.start(intent);
        if (UserManager.getInstance().isLogin()) {
            Intent loginIntent = new Intent(getUiControlInterface().getContextInternal(),
                    MainActivity.class);
            getUiControlInterface().launchActivity(true, loginIntent);
            getUiControlInterface().finish();
        }
    }

    private boolean doCheckData(LoginDataTo loginDataTo) {
        String phoneNumber = loginDataTo.getMobile();
        if (!CheckParamUtil.isMobile(phoneNumber)) {
            getUiControlInterface().showToast(getString(R.string.pls_input_right_phone_number));
            return false;
        }
        String authCode = loginDataTo.getCode();
        if (!CheckParamUtil.checkNumber(authCode, 4, 6)) {
            getUiControlInterface().showToast(getString(R.string.pls_input_right_auth_code));
            return false;
        }
        return true;
    }

    public void doLogin(LoginDataTo loginDataTo) {
        if (!doCheckData(loginDataTo)) {
            return;
        };
        getUiControlInterface().showProgress(getString(R.string.logining));
        UserManager.getInstance().doLogin(loginDataTo, new DataListener<String>() {
            @Override
            public void onSuccess(String data, String message) {
                Intent loginIntent = new Intent(getUiControlInterface().getContextInternal(),
                        MainActivity.class);
                getUiControlInterface().launchActivity(true, loginIntent);
                getUiControlInterface().finish();
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {
                getUiControlInterface().dismissProgress();
            }
        });
    }

    public void doSendAuthenticationCode(String phoneNumber) {
        UserManager.getInstance().sendMessageVerifyCode(phoneNumber, new DataListener<Object>() {
            @Override
            public void onSuccess(Object data, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {
                ((LoginActivity)getUiControlInterface()).startCountDown();
            }
        });
    }
}
